import 'dart:io';
import 'package:device_info_plus/device_info_plus.dart';
import 'package:flutter/material.dart';
import 'package:package_info_plus/package_info_plus.dart';

// App包信息管理
class PackageInfoManager {
  static final PackageInfoManager _instance = PackageInfoManager._internal();

  factory PackageInfoManager() => _instance;

  PackageInfoManager._internal();

  /// App名称
  String? _appName;

  /// App版本号
  String? _appVersion;

  /// App Build版本号
  String? _appBuild;

  /// 系统名称（iOS/Android）
  String? _systemName;

  /// 系统版本（15.0）
  String? _systemVersion;

  String? _model;

  String? _userAgent;

  String? _acceptLanguage;

  static String? get appName => _instance._appName;

  static String? get appVersion => _instance._appVersion;

  static String? get appBuild => _instance._appBuild;

  static String? get systemName => _instance._systemName;

  static String? get systemVersion => _instance._systemVersion;

  static String? get model => _instance._model;

  static String? get userAgent => _instance._userAgent;

  static String? get acceptLanguage => _instance._acceptLanguage;

  getPackageInfo() async {
    PackageInfo packageInfo = await PackageInfo.fromPlatform();
    _appName = packageInfo.appName;
    _appVersion = packageInfo.version;
    _appBuild = packageInfo.buildNumber;
    // String packageName = packageInfo.packageName;// 包名

    debugPrint("【App信息】 名称:$_appName  版本:$_appVersion  Build:$_appBuild");

    // 设备信息
    DeviceInfoPlugin deviceInfoPlugin = DeviceInfoPlugin();

    if (Platform.isAndroid) {
      AndroidDeviceInfo androidInfo = await deviceInfoPlugin.androidInfo;

      _systemName = 'Android';
      _systemVersion = androidInfo.version.release;
      _model = androidInfo.model;
      debugPrint('Running on ${androidInfo.model} ${androidInfo.toMap()}');
    }
    if (Platform.isIOS) {
      IosDeviceInfo iosInfo = await deviceInfoPlugin.iosInfo;
      _systemName = 'iOS';
      _systemVersion = iosInfo.systemVersion;
      _model = iosInfo.model;
      debugPrint('Running on ${iosInfo.utsname.machine}   ${iosInfo.toMap()}');
    }

    _userAgent =
        'Mozilla/5.0 ($_systemName $_systemVersion) InmotionApp/$_appVersion InmotionAPI/3';
  }

  setAcceptLanguage(List<Locale>? localeList) {
    if (_acceptLanguage != null) {
      return;
    }
  }
}
